/*
字典
*/
class Dictionary {
  constructor() {
    this.table = {}
  }
  hasKey(key) {
    return this.table[keyToString(key)] != null
  }
  set(key, value) {
    if(key != null && value != null) {
      let table_key = keyToString(key)
      this.table[table_key] = new ValuePair(key, value)
      return true
    }
    return false
  }
  remove(key) {
    if(this.hasKey(key)) {
      delete this.table[keyToString(key)]
      return true
    }
    return false
  }
  get(key) {
    if(this.hasKey(key)) {
      let table_key = keyToString(key)
      return this.table[table_key].value
    }
    return undefined
  }
  keyValues() {
    return Object.values(this.table)
  }
  keys() {
    return this.keyValues().map(valuePair=> valuePair.key)
  }
  values() {
    return this.keyValues().map(valuePair=> valuePair.value)
  }
  forEach(callFn) {
    let valuePairs = this.keyValues()
    for(let i = 0; i < valuePairs.length; i++) {
      let result = callFn(valuePairs[i].key, valuePairs[i].value)
      if(result === false) break;
    }
  }
  size() {
    return Object.keys(this.table).length;
  }
  isEmpty() {
    return this.size() === 0
  }
  clear() {
    this.table = {}
  }
}
function keyToString(item) {
    if(item === null) {
      return 'NULL'
    }
    if(item === undefined) {
      return 'UNDEFINED'
    }
    if(item instanceof String) {
      return `${item}`
    }
    return item.toString()
  }
  
  module.exports = Dictionary